package com.example.androidproject.chat;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androidproject.R;
import com.example.androidproject.chat.pojo.ChatGroupInfo;
import com.example.androidproject.databinding.ItemChatListBinding;
import com.example.androidproject.sqlite.GroupRepository;

import java.util.ArrayList;
import java.util.List;
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private List<GroupRepository.GroupRecord> list;
    private OnItemClickListener onItemClickListener;  // 点击事件接口
    public void updateList(List<GroupRepository.GroupRecord> list){
        this.list = list;
        notifyDataSetChanged();
    }
    // 构造函数
    public ChatAdapter(List<GroupRepository.GroupRecord> list, OnItemClickListener onItemClickListener) {
        this.list = list;
        this.onItemClickListener = onItemClickListener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemChatListBinding binding = ItemChatListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GroupRepository.GroupRecord groupRecord = list.get(position);
        holder.bind(groupRecord);
        holder.itemView.setOnClickListener(v -> {
            // 在这里执行跳转逻辑
            Intent intent = new Intent(v.getContext(), ChatActivity.class);
            intent.putExtra("CHAT_GROUP_INFO", groupRecord.getGroupName());
            v.getContext().startActivity(intent);
        });
        // 添加点击缩放动画效果
        holder.itemView.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.animate().scaleX(0.95f).scaleY(0.95f).setDuration(100).start();
                    return true;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    v.animate().scaleX(1f).scaleY(1f).setDuration(100).start();
                    v.performClick();  // 确保点击事件被触发
                    return true;
                default:
                    return false;
            }
        });
    }
    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
    // 点击事件接口
    public interface OnItemClickListener {
        void onItemClick(GroupRepository.GroupRecord groupRecord);
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        ItemChatListBinding binding;
        public ViewHolder(@NonNull ItemChatListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
        public void bind(GroupRepository.GroupRecord groupRecord) {
            binding.textName.setText(groupRecord.getUsername());
            binding.textIp.setText(groupRecord.getIp());
            // 设置点击监听器
            binding.getRoot().setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(groupRecord);
                }
            });
        }
    }
}